package ano.it.controller;

import ano.it.constant.MessageConstant;
import ano.it.entity.PageResult;
import ano.it.entity.QueryPageBean;
import ano.it.entity.Result;
import ano.it.pojo.GreenHouse;
import ano.it.service.GreenHouseService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/greenhouse")
public class GreenHouseController {
    @Reference
    private GreenHouseService greenHouseService;

    @RequestMapping("/add")
    public Result add(@RequestBody GreenHouse greenHouse, Integer deviceIds[]){
        try {
            greenHouseService.add(greenHouse,deviceIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_GREENHOUSE_FALL);
        }
        return new Result(true,MessageConstant.ADD_GREENHOUSE_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return greenHouseService.findPage(queryPageBean);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            greenHouseService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_GREENHOUSE_FALL);
        }
        return new Result(true, MessageConstant.DELETE_GREENHOUSE_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            GreenHouse greenHouse=greenHouseService.findById(id);
            return new Result(true,MessageConstant.QUERY_DATA_SUCCESS,greenHouse);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_DATA_FAIL);
        }
    }
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> data =greenHouseService.findDeviceIdsByGreenHouseId(id);
            return new Result(true,MessageConstant.QUERY_DEVICE_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_DEVICE_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody GreenHouse greenHouse,Integer deviceIds[]){
        try {
            greenHouseService.edit(greenHouse,deviceIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_GREENHOUSE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_GREENHOUSE_SUCCESS);
    }

    @RequestMapping("/getReportData")
    public Result getReportData(Integer id){
        try {
            GreenHouse greenHouse=greenHouseService.getReportData(id);
            return new Result(true,MessageConstant.GET_REPOT_SUCCESS,greenHouse);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_REPOT_FAIL);
        }

    }


}
